<template>
    <div class="searchForproducts">
        <!-- 导航栏 -->
        <van-nav-bar
            left-text="返回"
            left-arrow
            class="sticky"
            @click-left="back"
        >
            <template #right>
                <van-search
                    v-model="value"
                    show-action
                    shape="round"
                    :clearable="false"
                    placeholder="请输入搜索关键词"
                    @search="onSearch"
                    @focus="focus"
                    @blur="blur"
                />
            </template>
        </van-nav-bar>

        <!-- 搜索历史盒 -->
        <div class="searchHistory_box" :class="{ goHistory: isShow }">
            <div class="searchHistory">
                <div class="title">
                    搜索历史
                    <van-icon name="delete-o" @click="delSearchHistory" />
                </div>
            </div>
            <div class="history">
                <div
                    class="element"
                    v-for="(item, index) in searchHistory"
                    :key="index"
                    @click="clickSearch(index)"
                >
                    {{ item }}
                </div>
            </div>
        </div>

        <div>
            <Bg>
                <div class="searchForproducts-content">
                    <div class="searchForproducts-content-box">
                        <div
                            class="searchForproducts-box"
                            v-for="(item, index) in searchForproductsData"
                            :key="index"
                        >
                            <ProItem :information="item" class="proItem" />
                        </div>
                    </div>
                    <van-empty
                        v-if="searchForproductsData.length === 0"
                        description="请输入商品中文名称的关键词!"
                        image="search"
                    />
                </div>
            </Bg>
        </div>
    </div>
</template>

<script>
// 导入公共组件
import Back from "../components/Back.vue";
import Bg from "../components/Bg.vue";
import ProItem from '../components/ProItem.vue'
import { Dialog } from 'vant';
export default {
    name: "SearchForproducts",
    // 注册子组件
    components: {
        Back,
        Bg,
        ProItem
    },
    data() {
        return {
            // 搜索到的商品数据
            searchForproductsData: [],
            // 搜索词
            value: '',

            // 搜索历史
            searchHistory: [],
            // 搜索历史 开关
            isShow: false,
            // 判断登录
            enroll: false,
        }
    },
    methods: {
        //返回
        back() {
            //返回上一级
            this.$router.back();
        },
        onSearch(val) {
            if (this.value.trim()) {
                this.getSearch(val);
            } else { this.value = "" }
        },

        // 搜索商品
        getSearch(name) {

            this.axios({
                method: 'GET',
                url: "/search",
                params: { name }

            }).then((result) => {
                

                // 请求成功， 存储数据
                if (result.data.code === 'Q001') {
                    this.searchForproductsData = result.data.result;
                    

                    // 判断是否搜索到数据， 有就添加搜索记录
                    if (this.searchForproductsData.length > 0 && this.enroll) {
                        // 寻找相同的记录
                        for (let i = 0; i < this.searchHistory.length; i++) {
                            if (this.searchHistory[i] === name) {
                                this.searchHistory.splice(i, 1);
                                break
                            }
                        }
                        this.searchHistory.unshift(name);
                        if (this.searchHistory.length > 10) {
                            this.searchHistory.pop();
                        }

                        sessionStorage.setItem("searchHistory", JSON.stringify(this.searchHistory));
                    }
                }

            }).catch((err) => {
                
            });
        },

        // 输入框获得焦点时触发
        focus() {
            if (this.searchHistory.length !== 0 && this.enroll) {
                this.isShow = true;
            }
        },

        // 输入框失去焦点时触发
        blur() {
            if (this.searchHistory.length !== 0 && this.enroll) {
                this.isShow = false;
            }
        },

        // 删除历史记录
        delSearchHistory() {
            this.isShow = true;

            Dialog.confirm({
                message: '确定删除全部搜索历史？'
            }).then(() => {
                this.isShow = false;
                this.searchHistory = [];
                 sessionStorage.setItem("searchHistory", JSON.stringify(this.searchHistory));
            }).catch(() => { });
        },

        // 点击记录进行搜索
        clickSearch(i) {
            
            if (this.searchHistory[i] === this.value.trim()) {
                return
            }
            this.value = this.searchHistory[i];
            this.getSearch(this.searchHistory[i]);
        },

        // 获取登录状态
        getFindMy() {
            this.axios({
                method: "GET",
                url: "/findMy"
            }).then((result) => {
                

                // 已经登录
                if (result.data.code === "A001") {
                    this.enroll = true;
                }
                this.getSearch(this.$route.query.val);
            }).catch((err) => {
                
            });
        }
    },

    // 初始化
    created() {

        this.getFindMy();
        
        this.value = this.$route.query.val;

        // 获取搜索记录
        this.searchHistory = JSON.parse(sessionStorage.getItem("searchHistory"));
        if (!this.searchHistory) {
            this.searchHistory = [];
        }
    },
}
</script>

<style lang="less" scoped>
.searchForproducts {
    position: fixed;
    top: 0;
    bottom: 0;
    left: 0;
    right: 0;
    background-color: #f7f7f7;
    overflow: scroll;
    .sticky {
        position: sticky;
        top: 0;
    }
    /deep/ .van-search--show-action {
        padding: 0;
    }
    /deep/ .van-nav-bar__left {
        span,
        i {
            color: #0c34ba;
        }
    }
    /deep/ .sticky {
        z-index: 5;
    }
    .goHistory {
        top: 46px !important;
    }
    // 搜索历史
    .searchHistory_box {
        position: absolute;
        top: -62px;
        transition: top 1s ease-out;
        z-index: 2;

        width: 100%;
        max-height: 108px;
        background-color: #fff;
        padding: 8px 10px;
        border-bottom-right-radius: 10px;
        border-bottom-left-radius: 10px;

        .searchHistory {
            .title {
                color: #666;
                display: flex;
                align-items: center;
                i {
                    color: #666;
                    margin-left: 5px;
                    font-size: 17px;
                }
            }
        }

        .history {
            display: flex;
            flex-wrap: wrap;

            .element {
                max-width: 100px;
                border: 1px solid #d9d9d9;
                padding: 1px 8px;
                margin-right: 10px;
                margin-top: 10px;
                border-radius: 20px;
                color: #333333;
                font-size: 13px;

                overflow: hidden;
                white-space: nowrap;
                text-overflow: ellipsis;
                word-break: break-all;
            }
        }
    }

    /deep/ .bg-box {
        height: 150px;
    }

    // 搜索到的商品盒
    .searchForproducts-content {
        .searchForproducts-content-box {
            display: flex;
            padding: 10px;
            padding-bottom: 0;
            flex-wrap: wrap;
        }

        .searchForproducts-box {
            width: calc(~'33.333% - 9px + 3px');
            margin-right: 9px;
            margin-bottom: 12px;
            background-color: #fff;

            &:nth-child(3n) {
                margin-right: 0;
            }
        }
    }
}
</style>